<?php

if (!isset($_POST['id'])) header("Location: http://{$_SERVER['HTTP_HOST']}/dict/wordtest.php");

//This is stage 2. For each test mode, check if the given answers are correct, and prepare a form for submission to stage 3 (parameters: id[] = sid; l(sid) = language; s(sid) = score). Users can modify their own score to account for imperfections in the system.

require_once("common.inc.php");
require_once("db.inc.php");
require_once("queries.inc.php");

auth();
$dbh = get_db();

initSession();

xmlhead("Woorden overhoren - Grade answers");

$languages = getLanguages();

?>
<body>
<form method="post" action="wordtest_submit.php">
<p>
<input type="hidden" name="interface_language" value="<?php echo $_SESSION['interface_language']; ?>" />
<input type="hidden" name="learn_language" value="<?php echo $_SESSION['learn_language']; ?>" />
<input type="hidden" name="test_mode" value="<?php echo $_SESSION['test_mode']; ?>" />

<table>
<tr>
	<th>Question
	<th>Answer
	<th>+
	<th>0
	<th>-
<?php

for ($i = 0; $i < count($_POST['id']); $i++) {
	if (isset($_POST['id'][$i]) && is_numeric($_POST['id'][$i])) {
		$q_id = $_POST['id'][$i];
		$answer = mysql_real_escape_string($_POST["a$q_id"], $dbh);
	} else {
		die ("illegal input!");
	}
	
	//get defined meaning and spelling of this syntrans
	$query = mysql_query(
		"SELECT dm.defined_meaning_id, spelling
		FROM uw_defined_meaning dm, uw_syntrans s, uw_expression e
		WHERE s.syntrans_sid = $q_id
		AND s.expression_id = e.expression_id
		AND s.defined_meaning_id = dm.defined_meaning_id
		AND dm.remove_transaction_id IS NULL
		AND s.remove_transaction_id IS NULL
		AND e.remove_transaction_id IS NULL
		", $dbh
	) or die (mysql_error());
	
	if (mysql_num_rows($query) != 1)
		die (mysql_num_rows($query) . " matching dm's/expressions found for syntrans $q_id; expected exactly 1");
	$src_dm = mysql_result($query,0,0);
	$src_spelling = mysql_result($query,0,1);
	
	if ($_SESSION['test_mode'] == 1) {
	
		//find all correct answers (TODO: take into account shelves - maybe something with finding $taboo_answers first?)
		$query = mysql_query(
			"SELECT syntrans_sid, language_id, spelling
			FROM uw_syntrans s, uw_expression e, uw_defined_meaning dm
			WHERE
			s.expression_id = e.expression_id
			AND s.defined_meaning_id = dm.defined_meaning_id
			AND s.remove_transaction_id IS NULL
			AND e.remove_transaction_id IS NULL
			AND dm.remove_transaction_id IS NULL
			AND identical_meaning = 1
			AND language_id != {$_SESSION['learn_language']}
			AND dm.defined_meaning_id = $src_dm
		", $dbh
		) or die (mysql_error());
		
		if (mysql_num_rows($query) > 0) {
			//a valid answer may be the same in multiple languages, so we have to catch the possibility of >1 row
			
			$correct_answer_given = false;
			$correct_answer_sid = 0;
			$correct_answer = "";
			$potential_languages = array();
			$answer_language = 0;
			
			while (($row = mysql_fetch_array($query)) && !$correct_answer_given) {
				if ($answer === $row['spelling']) { //see if the $answer is a valid translation for the $dm
					$correct_answer_given = true;
					$correct_answer_sid = $row['syntrans_sid'];
					$correct_answer = $row['spelling'];
					$answer_language = $row['language_id'];
				}
				$answer_language = $row['language_id']; //any language will do if there's no preferred language
			}
			
			if ($correct_answer_given) {
				?>
	<tr>
	<td><?php echo $src_spelling; ?>
	<td><span class="correct_answer"><?php echo $correct_answer; /* $answer === $row['spelling'], right? */ ?></span>
	<td><input type="hidden" name="s<?php echo $q_id; ?>" value="1" />
	<td>
	<td>
	<input type="hidden" name="h<?php echo $q_id; ?>" value="<?php echo $correct_answer_sid; ?>" />
	<input type="hidden" name="l<?php echo $q_id; ?>" value="<?php echo $answer_language; ?>" />
	<input type="hidden" name="id[]" value="<?php echo $q_id; ?>" />


	<?php
			} else { //correct answer not given, find improvement of preference
				$correct_answer = get_expression_for_dm($src_dm);
				$correct_answer_expression = $correct_answer['spelling'];
				$correct_answer_sid = $correct_answer['syntrans_sid'];
				?>
	<tr>
	<td><?php echo $src_spelling; ?>
	<td><span class="wrong_answer"><?php echo $answer; ?></span> <i><?php echo $correct_answer_expression; ?></i>
	<td><input type="radio" name="s<?php echo $q_id; ?>" value="1" />
	<td><input type="radio" name="s<?php echo $q_id; ?>" value="0" />
	<td><input type="radio" name="s<?php echo $q_id; ?>" value="-1" checked="checked" />
	<input type="hidden" name="h<?php echo $q_id; ?>" value="<?php echo $correct_answer_sid; ?>" />
	<input type="hidden" name="l<?php echo $q_id; ?>" value="<?php echo $answer_language; ?>" />
	<input type="hidden" name="id[]" value="<?php echo $q_id; ?>" />

	<?php
			}
		} else { //no possible correct answers
			die ("dm $src_dm has translations into only one language! Please add some.");
		}
	} //end if(test_mode == 1)
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////
	
	else if ($_SESSION['test_mode'] == 2) {
		
		//TODO: don't reget this from DB, but pass it as a POST variable from previous stage (wordtest.php)
		$question = get_expression_for_dm($src_dm);
		$question_text = str_replace("%'%", "\'", get_text_for_dm($src_dm));
		?>
<tr>
<td><?php echo "{$question['spelling']} (<a href=\"javascript:alert('$question_text')\">description</a>)";
		
		if ($answer === $src_spelling) { ?>
<td><span class="correct_answer"><?php echo $answer ?>
<td><input type="hidden" name="s<?php echo $q_id; ?>" value="1" />
<td>
<td>
<input type="hidden" name="h<?php echo $q_id; ?>" value="<?php echo $question['syntrans_sid']; ?>" />
<input type="hidden" name="l<?php echo $q_id; ?>" value="<?php echo $question['language_id']; ?>" />
<input type="hidden" name="id[]" value="<?php echo $q_id; ?>" />

<?php
		} else {
			if ($alternative_syntrans = is_valid_alternative_for_syntrans($answer, $q_id, $src_dm)) {
				?>
<td><span class="alternative_answer"><?php echo $answer; ?></span> (also OK: <i><?php echo $src_spelling; ?></i>)
<td><input type="hidden" name="s<?php echo $alternative_syntrans; ?>" value="1" />
<td>
<td>
<input type="hidden" name="h<?php echo $alternative_syntrans; ?>" value="<?php echo $question['syntrans_sid']; ?>" />
<input type="hidden" name="l<?php echo $alternative_syntrans; ?>" value="<?php echo $question['language_id']; ?>" />
<input type="hidden" name="id[]" value="<?php echo $alternative_syntrans; ?>" />

<?php
			} else {
				?>
<td><span class="wrong_answer"><?php echo $answer; ?></span> <i><?php echo $src_spelling; ?></i>
<td><input type="radio" name="s<?php echo $q_id; ?>" value="1" />
<td><input type="radio" name="s<?php echo $q_id; ?>" value="0" />
<td><input type="radio" name="s<?php echo $q_id; ?>" value="-1" checked="checked" />
<input type="hidden" name="h<?php echo $q_id; ?>" value="<?php echo $question['syntrans_sid']; ?>" />
<input type="hidden" name="l<?php echo $q_id; ?>" value="<?php echo $question['language_id']; ?>" />
<input type="hidden" name="id[]" value="<?php echo $q_id; ?>" />

<?php
			}
		}
	} //end if(test_mode == 2)
} //end for each answer

?>
</table>
<input type="submit" value="Upload score" />
</p>
</form>
<?php
xmlfoot();
?>